﻿var yid = null;
var hasLogo = false;
var hasFrame = false;
var hasFigure = false;
var num = 0, lg = 0, fra = 0, fig = 0, count = 0;
var delWhich = '';
$(document).ready(function() {
    var $btnDel = $("#btnDel");
    $(":checkbox").click(function(event) {
        if ($(this).attr("checked")) {
            $btnDel.removeAttr("disabled");
            delWhich += event.target.id + '|';
            count++;
        }
        else {
            count--;
            delWhich = delWhich.replace(event.target.id + '|', "");
            if (count == 0) {
                $btnDel.attr("disabled", "disabled");
            }
        }
    });
    yid = getYid();
    var sub = $("#btnConfirm");
    $("#uploadify").uploadify({
        'uploader': '../swf/uploadify.swf',
        'script': escape('Upload.ashx?yid=' + yid + "&logo=yes"),
        'cancelImg': '../images/cancel.gif',
        'folder': '../logos',
        'queueID': 'fileQueue',
        'multi': false,
        'fileDesc': 'Flash动画文件',
        'fileExt': '*.swf;',
        'wmode': 'transparent',
        'sizeLimit': 102400,
        'onSelect': function(event, queueID, fileObj) {
            lg = 0;
            sub.attr("disabled", "disabled");
            if (fileObj.size >= 102400) {
                alert("文件大小超出限制,请重新选择!");
                $(this).uploadifyCancel(queueID);
            }
            else {
                hasLogo = true;
                lg++;
                sub.removeAttr("disabled");
            }
        },
        'onCancel': function() {
            lg = 0;
            hasLogo = false;
        },
        'onProgress': function() {
            sub.attr("disabled", "disabled");
        },
        'onComplete': function(event, queueID, fileObj, response, data) {
            if (hasLogo) {
                complete();
            }
        }
    });
    $("#uploadFrame").uploadify({
        'uploader': '../swf/uploadify.swf',
        'script': escape('Upload.ashx?yid=' + yid + "&frame=yes"),
        'cancelImg': '../images/cancel.gif',
        'folder': '../logos',
        'queueID': 'frameQueue',
        'multi': false,
        'fileDesc': 'jpg图片',
        'fileExt': '*.jpg;',
        'wmode': 'transparent',
        'sizeLimit': 102400,
        'onSelect': function(event, queueID, fileObj) {
            fra = 0;
            sub.attr("disabled", "disabled");
            if (fileObj.size >= 102400) {
                alert("文件大小超出限制,请重新选择!");
                $(this).uploadifyCancel(queueID);
            }
            else {
                hasFrame = true;
                fra++;
                sub.removeAttr("disabled");
            }
        },
        'onCancel': function() {
            fra = 0;
            hasFrame = false;
        },
        'onProgress': function() {
            sub.attr("disabled", "disabled");
        },
        'onComplete': function(event, queueID, fileObj, response, data) {
            if (hasFrame) {
                complete();
            }
        }
    });
    $("#uploadFigure").uploadify({
        'uploader': '../swf/uploadify.swf',
        'script': escape('Upload.ashx?yid=' + yid + "&figure=yes"),
        'cancelImg': '../images/cancel.gif',
        'folder': '../logos',
        'queueID': 'figureQueue',
        'multi': false,
        'fileDesc': 'Flash动画文件',
        'fileExt': '*.swf;',
        'wmode': 'transparent',
        'sizeLimit': 102400,
        'onSelect': function(event, queueID, fileObj) {
            fig = 0;
            sub.attr("disabled", "disabled");
            if (fileObj.size >= 102400) {
                alert("文件大小超出限制,请重新选择!");
                $(this).uploadifyCancel(queueID);
            }
            else {
                hasFigure = true;
                fig++;
                sub.removeAttr("disabled");
            }
        },
        'onCancel': function() {
            fig = 0;
            hasFigure = false;
        },
        'onProgress': function() {
            sub.attr("disabled", "disabled");
        },
        'onComplete': function(event, queueID, fileObj, response, data) {
            if (hasFigure) {
                complete();
            }
        }
    });
    var complete = function() {
        num++;
        if (num == lg + fra + fig) {
            upLoad('upload', "");
        }
    }

    sub.click(function() {
        if (hasLogo) {
            $("#uploadify").uploadifyUpload();
        }
        if (hasFrame) {
            $("#uploadFrame").uploadifyUpload();
        }
        if (hasFigure) {
            $("#uploadFigure").uploadifyUpload();
        }
    });
    $btnDel.click(function() {
        if (confirm('确定删除所选自定义信息?')) {
            upLoad(delWhich, '');
        }
    });
});

var MakeMessage = function(args, context) {
    var $responseMessage = $("#responseMessage");
    if (args == "success") {
        $responseMessage.html("操作已成功");
        /*parent.document.getElementById('top').src = 'top_header.aspx?time=' + new Date().getMilliseconds();*/
         parent.document.getElementById('top').src = 'top_header.aspx?time=' + Math.random() + '&&time=' + new Date().getMilliseconds();
         //alert(parent.document.getElementById('top').src);
        $("#btnConfirm").removeAttr("disabled");
        parent.document.execCommand("refresh");
    }
    else if (args = "reLogin") {
        alert("删除成功,自动跳转到首页");
        top.location = "../iframe.aspx";
    }
    else {
        $responseMessage.html("操作失败,请重新尝试");
    }
}

var MakeError = function(args, context) {
    $("#responseMessage").html("发生错误...请重新尝试");
}
